package com.eight.cloud.message.service.impl;


import com.eight.cloud.core.utils.format.ObjectFormat;
import com.eight.cloud.message.service.IMessageService;
import com.eight.cloud.openfeign.message.model.MqMessage;
import com.eight.cloud.openfeign.message.model.SocketMessage;
import com.eight.cloud.rabbitmq.config.RabbitMqPublisher;
import com.eight.cloud.redisson.utils.RedisListenerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.eight.cloud.openfeign.message.constant.SocketConstant.WEBSOCKET_MESSAGE;

/**
 * @ClassName: IMessageServiceImpl
 * @Author: TXC
 * @Date: 2024-10-11 13:59
 **/
@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {
    @Resource
    private RedisListenerUtils listenerUtils;

    @Resource
    private RabbitMqPublisher rabbitMqPublisher;

    @Override
    public void sendWebSocketMessage(SocketMessage<?> socketMessage) {
        listenerUtils.createActuator(WEBSOCKET_MESSAGE).publish(ObjectFormat.objectToStr(socketMessage));
    }

    @Override
    public boolean sendRabbitMessage(MqMessage<?> mqMessage) {
        boolean result = rabbitMqPublisher.send(mqMessage.getBindingName(), mqMessage.getRoutingKey(),
                mqMessage.getPayload(), mqMessage.getSendStrategy());
        if (result) {
            log.info("消息发送成功，消息内容：{}", mqMessage);
        } else {
            log.info("消息发送失败，消息内容：{}", mqMessage);
        }
        return result;
    }
}
